package com.zlzkj.app.controller;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.zlzkj.app.model.Admin;
import com.zlzkj.app.model.Role;
import com.zlzkj.app.model.RoleNode;
import com.zlzkj.app.service.AdminRoleService;
import com.zlzkj.app.service.AdminService;
import com.zlzkj.app.service.RoleNodeService;
import com.zlzkj.app.service.RoleService;
import com.zlzkj.app.util.StringUtil;
import com.zlzkj.app.util.UIUtils;
import com.zlzkj.app.util.UploadUtils;
import com.zlzkj.core.base.BaseController;
import com.zlzkj.core.sql.Row;

/**
 * 公共控制器
 */
@Controller
@RequestMapping(value={"public"})
public class PublicController extends BaseController{

	@Autowired
	private AdminService adminService;
	
	@Autowired
	private AdminRoleService adminRoleService;
	
	@Autowired
	private RoleNodeService roleNodeService;
	
/*	@RequestMapping(value = "upload",method=RequestMethod.POST)
	public String upload(HttpServletRequest request,
						 HttpServletResponse response,
						 @RequestParam("imgFile") MultipartFile picWebFile){
		System.out.println("upload");
		Map<String, Object> data = new HashMap<String, Object>();
		String picWeb = ""; //图片保存名
		System.out.println("aaa");
		Map<String,Object> picWebInfo = UploadUtils.saveMultipartFile(picWebFile);
		System.out.println("bbb");
		if((Integer)picWebInfo.get("status")>0){ //上传完成
			picWeb =  UploadUtils.parseFileUrl(picWebInfo.get("saveName").toString());
		}else{ //上传出错
			return ajaxReturn(response,null,picWebInfo.get("errorMsg").toString(),0);
		}
		data.put("url", picWeb);
		data.put("alt", "");
		return ajaxReturn(response,data,null,1);
	}*/

	/**
	 * 登录
	 */
	@RequestMapping(value={"login"})
	public String login(Model model, HttpServletRequest request, HttpServletResponse response) {
		if(request.getMethod()=="POST"){
			String loginName = request.getParameter("loginName");
			String loginPwd = request.getParameter("loginPass");
			if( adminService.checkLogin(loginName, loginPwd)!=null){
				Admin admin = adminService.checkLogin(loginName, loginPwd);
//				request.getSession().setAttribute("loginName", loginName);//登录成功放入session的内容，供前端页面访问
				request.getSession().setAttribute("roleId", adminRoleService.find(adminService.findByName(loginName).toString()));//获得角色id
				request.getSession().setAttribute("admin", admin);//保存管理员信息到session中
				return ajaxReturn(response,null,"登录成功",1);
			}
			else 
				return ajaxReturn(response,null,"账号或密码错误",0);
		}else{
			return "public/login";
		}
//		return "public/login";
	}
	
	@RequestMapping(value={"logout"})
	public String logout(Model model, HttpServletRequest request, HttpServletResponse response){
		Enumeration<String> em = request.getSession().getAttributeNames();
//		System.out.println(request.getSession().getAttributeNames());
		while(em.hasMoreElements()){
			request.getSession().removeAttribute(em.nextElement());
		}
		request.getSession().invalidate();
		return "redirect:/public/login";
	}

}
